package com.cq.smartelebicycle;

import java.util.HashMap;

import com.cq.smartelebicycle.fragment.DateSettingsFragment;
import com.cq.smartelebicycle.fragment.DengGuangFragment;
import com.cq.smartelebicycle.fragment.FitnessFragment;
import com.cq.smartelebicycle.fragment.GPSFragment;
import com.cq.smartelebicycle.fragment.MusicFragment;
import com.cq.smartelebicycle.fragment.SpeedFragment;
import com.cq.smartelebicycle.fragment.WifiFragment;
import com.cq.smartelebicycle.fragment.BluetoothFragment;
import com.cq.smartelebicycle.fragment.WelcomeFragment;
import com.cq.smartelebicycle.fragment.ZhuliFragment;
import com.cq.smartelebicycle.service.IMediaPlaybackService;
import com.cq.smartelebicycle.service.MediaPlaybackService;
import com.cq.smartelebicycle.view.FunctionView;
import com.cq.smartelebicycle.view.FunctionView.Area;
import com.cq.smartelebicycle.view.FunctionView.OnFuncViewClickListener;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

public class MainFragment extends FragmentActivity implements OnClickListener,
		OnFuncViewClickListener {

	private static final String TAG = "MainFragment";
	private FunctionView mFunctionView;
	
	private HashMap<String, Fragment> mFragments = new HashMap<String, Fragment>();
	
	private IMediaPlaybackService mService;

	private ServiceConnection autoshuffle = new ServiceConnection() {
		public void onServiceConnected(ComponentName classname, IBinder obj) {
			// we need to be able to bind again, so unbind
			mService = IMediaPlaybackService.Stub.asInterface(obj);
			try {
				mService.makeAutoShuffleList();
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		public void onServiceDisconnected(ComponentName classname) {
			mService = null;
		}
	};

	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.main);
		bindService((new Intent(this, MediaPlaybackService.class)), autoshuffle, Context.BIND_AUTO_CREATE);
		initFragment();
		Log.d("SIMMON", TAG + "onCreate");
		ImageView gpsBt = (ImageView) findViewById(R.id.gps_bt);
		gpsBt.setOnClickListener(this);

		ImageView dengBt = (ImageView) findViewById(R.id.light_bt);
		dengBt.setOnClickListener(this);
		
		ImageView zhuliBt = (ImageView) findViewById(R.id.drog_bt);
		zhuliBt.setOnClickListener(this);
		
		ImageView speedBt = (ImageView) findViewById(R.id.speed_bt);
		speedBt.setOnClickListener(this);
		
		mFunctionView = (FunctionView) findViewById(R.id.funcView);
		mFunctionView.setOnFuncViewClickListener(this);

	}

	private void initFragment() {
		WelcomeFragment fragment = new WelcomeFragment();
		replaceFragment(fragment);
	}

	public void replaceFragment(Fragment fragment) {
		getSupportFragmentManager().beginTransaction()
				.replace(R.id.contain_frame, fragment).commit();
	}

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		Log.d("SIMMON", TAG + "onClick");
		String key = null;
		int id = view.getId();
		switch (id) {
		case R.id.gps_bt:
			key = GPSFragment.TAG_ID;
			if (mFragments.get(key) == null) {
				mFragments.put(key, new GPSFragment());
			}
			break;
		case R.id.light_bt:
			key = DengGuangFragment.TAG_ID;
			if (mFragments.get(key) == null) {
				mFragments.put(key, new DengGuangFragment());
			}
			break;
		case R.id.drog_bt:
			key = ZhuliFragment.TAG_ID;
			if (mFragments.get(key) == null) {
				mFragments.put(key, new ZhuliFragment());
			}
			break;
		case R.id.speed_bt:
			key = SpeedFragment.TAG_ID;
			if (mFragments.get(key) == null) {
				mFragments.put(key, new SpeedFragment());
			}
			break;

		default:
			break;
		}
		
		if (key != null && mFragments.get(key) != null) {
			replaceFragment(mFragments.get(key));
		}

	}

	@Override
	public void onFuncViewClick(Area type) {
		// TODO Auto-generated method stub
		String key = null;
		switch (type) {
		case WIFI:
			key = WifiFragment.TAG_ID;
			//if (mFragments.get(key) == null) {
				mFragments.put(key, new WifiFragment());
			//}
			break;
		case SPORT:
			key = FitnessFragment.TAG_ID;
			if(mFragments.get(key) == null){
				mFragments.put(key, new FitnessFragment());
			}
			break;
		case MUSIC:
			key = MusicFragment.TAG_ID;
			if (mFragments.get(key) == null) {
				mFragments.put(key, new MusicFragment());
			}
			break;
		case OBD:
			key = DateSettingsFragment.TAG_ID;
			//if (mFragments.get(key) == null) {
				mFragments.put(key, new DateSettingsFragment());
			//}
			break;
		case BT:
			key = BluetoothFragment.TAG_ID;
			if (mFragments.get(key) == null) {
				mFragments.put(key, new BluetoothFragment());
			}
			break;
		}
		if (key != null && mFragments.get(key) != null) {
			replaceFragment(mFragments.get(key));
		}
		//Toast.makeText(this, key, 0).show();

	}
	
	public IMediaPlaybackService getMediaPlayService() {
		// TODO Auto-generated method stub
		return mService;
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (mFragments.get(MusicFragment.TAG_ID) != null) {
			if (((MusicFragment) mFragments.get(MusicFragment.TAG_ID)).onKeyDown(keyCode, event)) {
				return true;
			}
		}
		return super.onKeyDown(keyCode, event);
	}
	
	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (mFragments.get(MusicFragment.TAG_ID) != null) {
			if (((MusicFragment) mFragments.get(MusicFragment.TAG_ID)).onKeyUp(keyCode, event)) {
				return true;
			}
		}
		return super.onKeyUp(keyCode, event);
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		if (autoshuffle != null) {
			unbindService(autoshuffle);
		}
	}

}
